Skip to content

perf: Tokenizer no longer copies the source string - #647

Merged
iheitlager merged 2 commits into
mainfrom
perf/644-tokenizer-borrow-src
Aug 29, 2026
Merged

perf: Tokenizer no longer copies the source string#647
iheitlager merged 2 commits into
mainfrom
perf/644-tokenizer-borrow-src

Conversation

@iheitlager

Copy link
Copy Markdown
Member

Summary

  • Tokenizer::new did src.to_string(), heap-allocating and copying the entire input on every parse call even though the tokenizer only ever reads through byte-range slices (self.src.get(...)).
  • Verified empirically that neither the obvious fix (Tokenizer<'a> { src: &'a str }) nor the fallback (Cow<'a, str>) is viable: both trip make check-mvl-limit ("explicit lifetime is outside the qualified subset") since src/parser/tokenizer.rs is in the qualified subset banning lifetimes beyond function-scoped elision.
  • Instead, Tokenizer no longer stores the source at all — every scan method now takes src: &str as a parameter, keeping every lifetime function-scoped (satisfying mvl-limit) while eliminating the per-parse copy entirely.

Test plan

  • cargo test --lib — 959 passed
  • cargo test --test tokenizer_proptest --test extracted_sql_corpus --test corpus --test tier1 --test unit_parser — all passed
  • make check-mvl-limit — passes (previously would have failed for both &'a str and Cow<'a, str> alternatives, confirmed by direct experiment)
  • cargo clippy --all-targets -- -D warnings — clean
  • cargo fmt --check
  • cargo bench --bench compile_path — ran clean, no regressions

Closes #644

spend: matched estimate (small)


🤖 Analysis by Claude

Tokenizer::new previously did src.to_string(), heap-allocating and
copying the whole input on every parse call even though the tokenizer
only ever reads through byte-range slices. The obvious fix (a &'a str
field) and the fallback (Cow<'a, str>) both fail make check-mvl-limit
(verified empirically: both trip "explicit lifetime is outside the
qualified subset"), since tokenizer.rs is in the qualified subset that
bans lifetimes beyond function-scoped elision.

Instead, Tokenizer no longer stores the source at all: every method
takes src: &str as a parameter, keeping every lifetime function-scoped
and eliminating the copy entirely.

spend: matched estimate (small)
@iheitlager
iheitlager force-pushed the perf/644-tokenizer-borrow-src branch from 4c1fc69 to dc018bd Compare August 29, 2026 17:26
@iheitlager
iheitlager merged commit e971b8b into main Aug 29, 2026
6 checks passed
@iheitlager
iheitlager deleted the perf/644-tokenizer-borrow-src branch August 29, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: Tokenizer copies source string instead of borrowing &str

1 participant